package jmm;


import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

// 多线程单例模式
public class SingletonDemo {
    private static volatile SingletonDemo instance = null;

    private SingletonDemo() {
        System.out.println(Thread.currentThread().getName() + "\t构造单例");
    }

    // DCL Double Check Lock 双端检锁机制
    // 使用同步代码块
    // 锁前锁后都进行判断
    public static SingletonDemo getInstance() {
        if (instance == null) {
            synchronized (SingletonDemo.class) {
                if (instance == null)
                    instance =  new SingletonDemo();
            }
        }
        return instance;
    }

    public static void main(String[] args) {
//        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
//        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
        System.out.println(SingletonDemo.class);
        for (int i = 0; i < 10; i++) {
            new Thread(SingletonDemo::getInstance).start();
        }
    }
}
